import {Body, Controller, Get, HttpStatus, Post, Query, Res} from '@nestjs/common';
import {Response} from 'express';
import {EventService} from '../../service/event.service';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {EventCategoryTypeStatus, ResCode} from '../../common/constants';
import {getLogStr, uuid} from "../../common/utils";
import {Logger} from '../../utils/log4js';

const exception = (e) => {
    console.log(e);
    try {
        Logger.error(getLogStr(e))
    } catch(e){}
    return new CommonResponseDto(ResCode.ERROR, '服务异常', null);
}
@Controller('event')
export class EventController {
    constructor(private readonly eventService: EventService) {}

    @Get('/list')
    async getAllList(@Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const data = await this.eventService.getAll()
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Get('/category-list')
    async getAllCategory(@Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const data = await this.eventService.getAllEventCategory()
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Get('/event-type-list')
    async getAllEventTypeByCategoryId(@Query() query: {categoryId: string}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (!query.categoryId) {
            return new CommonResponseDto(ResCode.ERROR, 'categoryId 必填', null)
        }
        try {
            const data = await this.eventService.getAllEventTypeByCategoryId(query.categoryId)
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/create-category')
    async createCategory(@Body() params: { name: string }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        const id = uuid(32);
        params.name = params.name || '';
        if (!params.name) {
            return new CommonResponseDto(ResCode.ERROR, 'name 必填', null)
        }
        try {
            const exist = await this.eventService.getEventCategoryByName(params.name)
            if (exist) {
                return new CommonResponseDto(ResCode.ERROR, 'name已存在', null)
            }
            const result = await this.eventService.createEventCategory({id, name: params.name, status: EventCategoryTypeStatus.NORMAL})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/create-event-type')
    async createType(@Body() params: { categoryId: string, name: string, code: string}, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        const id = uuid(32);
        params.categoryId = params.categoryId || '';
        params.name = params.name || '';
        params.code = params.code || '';
        if (!params.name || !params.code || !params.categoryId) {
            return new CommonResponseDto(ResCode.ERROR, 'categoryId name code 必填', null)
        }
        try {
            const exist = await this.eventService.getEventTypeByNameOrCode(params.name, params.code)
            if (exist) {
                return new CommonResponseDto(ResCode.ERROR, 'name 或 code已存在', null)
            }
            const result = await this.eventService.createEventType({id, categoryId: params.categoryId, name: params.name, code: params.code, status: EventCategoryTypeStatus.NORMAL})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit-category')
    async editCategory(@Body() params: { id: string, name: string }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        params.name = params.name || '';
        const { id, name } = params;
        if (!id || !name) {
            return new CommonResponseDto(ResCode.ERROR, 'id name 必填', null)
        }
        try {
            const exist = await this.eventService.getEventCategoryByName(name)
            if (exist && exist.id !== id) {
                return new CommonResponseDto(ResCode.ERROR, '已存在记录', null)
            }
            const result = await this.eventService.editEventCategory({id, name})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit-event-type')
    async editType(@Body() params: { id: string, categoryId: string, name: string, code: string}, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        params.categoryId = params.categoryId || '';
        params.name = params.name || '';
        params.code = params.code || '';
        const { id, categoryId, name, code } = params;
        if (!id || !name || !categoryId || !code) {
            return new CommonResponseDto(ResCode.ERROR, 'id name categoryId code 必填', null)
        }
        try {
            const exist = await this.eventService.getEventTypeByNameOrCode(name, code)
            if (exist && exist.id !== id) {
                return new CommonResponseDto(ResCode.ERROR, '已存在记录', null)
            }
            const result = await this.eventService.editEventType({id, name, categoryId, code})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit-category-status')
    async editCategoryStatus(@Body() params: { id: string, status: EventCategoryTypeStatus }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        params.status = params.status || null;
        const { id, status } = params;
        if (!id || !status) {
            return new CommonResponseDto(ResCode.ERROR, 'id status 必填', null)
        }
        if (EventCategoryTypeStatus.NORMAL !== status && EventCategoryTypeStatus.DELETED !== status) {
            return new CommonResponseDto(ResCode.ERROR, 'status 值错误', null)
        }
        try {
            const result = await this.eventService.editEventCategoryStatus({id: params.id, status: params.status})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/edit-event-type-status')
    async editTypeStatus(@Body() params: { id: string, status: EventCategoryTypeStatus }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        params.status = params.status || null;
        const { id, status } = params;
        if (!id || !status) {
            return new CommonResponseDto(ResCode.ERROR, 'id status 必填', null)
        }
        if (EventCategoryTypeStatus.NORMAL !== status && EventCategoryTypeStatus.DELETED !== status) {
            return new CommonResponseDto(ResCode.ERROR, 'status 值错误', null)
        }
        try {
            const result = await this.eventService.editEventTypeStatus({id: params.id, status: params.status})
            return new CommonResponseDto(ResCode.OK, 'success', result)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/del-event-category')
    async delEventCategory(@Body() params: { id: string }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        const { id } = params;
        if (!id) {
            return new CommonResponseDto(ResCode.ERROR, 'id 必填', null)
        }
        try {
            const result = await this.eventService.delEventCategory(id)
            return new CommonResponseDto(ResCode.OK, 'success', null)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/del-event-type')
    async delEventType(@Body() params: { id: string }, @Res({ passthrough: true }) res: Response) {
        console.log(params)
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        const { id } = params;
        if (!id) {
            return new CommonResponseDto(ResCode.ERROR, 'id 必填', null)
        }
        try {
            const result = await this.eventService.delEventType(id)
            return new CommonResponseDto(ResCode.OK, 'success', null)
        } catch(e) {
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
